<template>
<div class="login_container">
    <div class="login_box">
        <div class="avatar">
            <img src="../assets/logo.png" alt="">
        </div>
        <!-- 登录表单 -->
        <el-form :model="loginForm" :rules="loginRules" label-width="0px" class="login_form" ref="loginRef" >
            <el-form-item prop="username" >
                <el-input v-model="loginForm.username"  prefix-icon="iconfont icon-UserSecret"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="iconfont icon-wodemima" type="password"></el-input>
            </el-form-item>
            <el-form-item class="login_btn">
                <el-button type="primary" @click="login">登录</el-button>
                <el-button type="info" @click="resetLogin">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
</template>
<script>
export default {
  data () {
    return {
      // 对象绑定
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 验证规则
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLogin () {
      this.$refs.loginRef.resetFields()
    },
    login () {
      this.$refs.loginRef.validate(async value => {
        //    验证通过value==true
        if (value) {
          //    发送请求
          const { data: res } = await this.$axios.post('login', this.loginForm)
          //   请求成功
          if (res.meta.status === 200) {
            this.$message.success(res.meta.msg)
            window.sessionStorage.setItem('token', res.data.token)
            this.$router.push('home')
          } else {
            //   请求失败直接return
            return this.$message.error(res.meta.msg)
          };
        } else {
          //    验证不通过直接停止运行
          return
        };
      })
    }
  }
}
</script>
<style lang="less" scoped>
.login_container{
    background-color: #2b4b6b;
    height: 100%;
}
.login_box{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    // 头像
    .avatar{
        height: 130px;
        width: 130px;
        border-radius: 50%;
        border:1px solid #eee;
        box-shadow: 0 0 10px #ddd;
        padding: 7px;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            border-radius: 50%;
            width: 100%;
            height: 100%;
            vertical-align:middle;
            background-color: #eee;
        }
    }
    // 登录表单
    .login_form{
        margin-top:100px;
        padding: 0 20px;

        .login_btn{
            display: flex;
            justify-content: flex-end;
        }
     }

}

</style>
